/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.utils.collections;

import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: CollectionUtil
 * @Author: Fynn Qi
 * @Date: 2021/2/5 15:15
 * @Version: V1.0
 */
public class CollectionUtil {

    public static boolean isExist(String element, List<String> elements) {
        if (elements == null || elements.size() <= 0) {
            return false;
        }
        return elements.stream().anyMatch(x -> x.equals(element));
    }

    public static void tryAdd(String element, List<String> elements) {
        boolean isExist = elements.stream().anyMatch(x -> x.equals(element));
        if (!isExist) {
            elements.add(element);
        }
    }

    public static void tryAddAll(List<String> sourceElements, List<String> targeElements) {
        if (sourceElements == null || sourceElements.size() <= 0 || targeElements == null) {
            return;
        }
        for (String sourceKey : sourceElements) {
            boolean isExist = targeElements.stream().anyMatch(x -> x.equals(sourceKey));
            if (!isExist) {
                targeElements.add(sourceKey);
            }
        }
    }

    /**
     * 获取List大小
     *
     * @param list List，可null
     * @return 0 or List.size()
     */
    public static int getListSize(List<?> list) {
        return list == null ? 0 : list.size();
    }
}
